<?php
namespace Pc\Controller;
use Think\Controller;
/**
 * UpdateController.class.php 生成待办数据处理类
 */
class UpdateController extends CommonController{
    /* 属性声明 */
    protected $userinfo;//员工数据


    /* 方法声明 */
    public function __construct() {
        parent::__construct();
        if(!$this->uuid = I('get.uuid') ){
            $this -> error('员工编码缺失!');
        }
        //dump($this->uuid);exit;
        //获取redis员工数据
        $this -> redis -> select(10);
        $userinfo = $this -> redis -> hget('user_list',$this->uuid);
        if(!$userinfo){
            $this -> error('未知成员uuid数据!');
        }
        $this->userinfo = json_decode($userinfo,true);

    }

    /**
     * 提交审核
     */
    public function index(){
        //1.获取成员更改数据
        $data = $this -> checkInfo($this->userinfo,$this->uuid);
        //dump($data);exit;
        $upd_info = $data['temp'];
        $items = $data['items'];

        //2.调用aws接口获取修改的子集与成员的关系
        $cmd = 'hr.separateInfo';
        $arr = ['boList' => $items];
        $json = json_encode($arr);
        //dump($json);exit;
        $nexus = A('Aws')->requestAws2($cmd,$json);
        //dump($nexus);
        if($nexus['type'] == 0){
            $this->error('分组接口错误!');
        }
        $nexus = $nexus['data'];
        //dump($nexus);exit;
        //3.调用aws接口根据成员生成相应的代办
        $row = $upd_info;
        $coun = 0;
        foreach($nexus as $k=>$v){
            if($awsid = A('Aws') -> dealt($this->uuid,$upd_info['user_name'],$k)){
                $coun++;
                //$awsid = rand(1000,9999);
                $list = [];
                $info = $upd_info['information'];
                foreach($v as $h=>$g){
                    $list[$g] = $info[$g];
                }
                $row['information'] = $list;

                //缓存当前用户流程的更改数据
                $this -> redis -> select(11);
                $this->redis->hset('change_'.$this->uuid,$awsid,serialize($row));
                //把更改的子集写入redis 哈希结构 hset(uuid, awsid, 子集名)
                $this->redis->hset('items_'.$this->uuid,$awsid,serialize($nexus[$k]));
                //
            }else{
                $this -> error('生成流程失败!');
            }
        }

        //去除提交的修改子集的审批标记
        $userlist = $this->userinfo['information'];
        //dump($userlist);exit;
        foreach($items as $k=>$v){
            if($v == 'BO_CASC_HR_YGRS'){
                unset($userlist[$v]['sp_type']);
                unset($userlist[$v]['sp_type']);
            }else{
                foreach($userlist[$v]['subvalue'] as $a=>$b){
                    unset($userlist[$v]['subvalue'][$a]['sp_type']);
                    unset($userlist[$v]['subvalue'][$a]['reason']);
                }
            }
        }
        $this->userinfo['information'] = $userlist;
        //dump($this->userinfo);exit;
        $this -> redis -> select(10);
        $this->redis->hset('user_list',$this->uuid,json_encode($this->userinfo));

        if(count($nexus) == $coun && $coun !=0){
            $this->success('提交审核成功');
        }else{
            $this -> error('提交审核失败!');
        }
    }

    /**
     * 分析处理成员数据
     * @param array $info 成员数据
     * @param string $corpid 企业号corpid
     */
    public function checkInfo($data,$uuid){
        $info = $data['information'];
        //dump($info);exit;
        $update_info = [];//更改后的数据
        $items = [];//修改的子集名

        //1 获取成员用户名和基本信息
        $user_name = $info['BO_CASC_HR_YGRS']['sublist']['YGXM']['value']; //员工姓名
        $bumen_name = $info['BO_CASC_HR_YGRS']['sublist']['SSZZ']['value']; //员工部门
        $zjlx_name = $info['BO_CASC_HR_YGRS']['sublist']['ZJLX']['value']; //证件类型
        $zjhm_name = $info['BO_CASC_HR_YGRS']['sublist']['ZJHM']['value']; //证件号码

        //2 整理员工基本信息数据
        if($info['BO_CASC_HR_YGRS']['change_type'] == 1){
            $items[] = 'BO_CASC_HR_YGRS';
            $list = $info['BO_CASC_HR_YGRS']['sublist'];
            $base_info = [];
            foreach($list as $k=>$v){
                if($v['change_type'] == 1){
                    $base_info[$k] = $v;
                }
            }
            $info['BO_CASC_HR_YGRS']['sublist'] = $base_info;
            $update_info['BO_CASC_HR_YGRS'] = $info['BO_CASC_HR_YGRS'];
        }
        //dump($update_info);exit;

        //3.整理可拓展子集数据
        foreach($info as $k => $v){
            if($k == 'BO_CASC_HR_YGRS'){
                continue;
            }

            //分析状态数据
            if($v['change_type'] == 1){
                $sx_val = [];
                $subvalue = $v['subvalue'];
                foreach($subvalue as $h=>$j){
                    if($j['change_type'] == 1){
                        $sx_val[$h] = $j;
                    }
                }
                $info[$k]['subvalue'] = $sx_val;
                $update_info[$k] = $info[$k];
                $items[] = $k;
            }
        }
        //dump($items);
        //dump($update_info);exit;

        //获取已经提交审核的子集项,把已经在审核的子集移除
        $this -> redis -> select(11);
        if($change_item = $this->redis->hvals('items_'.$this->uuid)){
            $str = '';
            foreach($change_item as $k=>$v){
                $v = unserialize($v);
                $str .= implode(',',$v).',';
            }
            $change_item = explode(',',rtrim($str,','));
            //dump($change_item);exit;
            //移除已在审核的子集
            foreach($update_info as $k=>$v){
                if(in_array($k,$change_item)){
                    unset($update_info[$k]);
                }
            }
            //移除保存的修改子集名里已在审核的子集名
            foreach($items as $k=>$v){
                if(in_array($v,$change_item)){
                    unset($items[$k]);
                }
            }
            $items = array_values($items);
        }
        //dump($update_info);exit;

        //4.最终整理数据
        if(!$update_info){
            $this -> error('您还未做任何更改!');
        }else{
            //绑定附件信息
            $update_info = $this->setFileInfo($update_info,$uuid);

            $temp['uuid'] = $this -> uuid;
            $temp['time'] = time();
            $temp['user_name'] = $user_name;
            $temp['bumen_name'] = $bumen_name;
            $temp['zjlx_name'] = $zjlx_name;
            $temp['zjhm_name'] = $zjhm_name;
            $temp['information'] = $update_info;

            return array(
                'temp' => $temp,
                'items' => $items
            );
        }

    }

    //绑定附件信息
    private function setFileInfo($update_info,$uuid){
        foreach($update_info as $k=>$v){
            $this->redis->select(9);
            $file_info = unserialize($this->redis->hget('file_'.$uuid,$k));
            //dump($file_info);exit;
            if($k == 'BO_CASC_HR_YGRS'){
                foreach($v['sublist'] as $k1=>$v1){
                    if($v1['type'] == 'FILE'){
                        $update_info[$k]['sublist'][$k1]['value'] = $file_info[0][$k1];
                    }
                }
            }else{
                //dump($file_info);exit;
                foreach($v['subvalue'] as $k1=>$v1){
                    foreach($v1['info'] as $k2=>$v2){
                        if($v2['type'] == 'FILE'){
                            $update_info[$k]['subvalue'][$k1]['info'][$k2]['value'] = $file_info[$k1][$k2];
                        }
                    }
                }
            }
        }
        //dump($update_info);exit;
        foreach($update_info as $k=>$v){
            if($k == 'BO_CASC_HR_YGRS'){
                foreach($v['sublist'] as $k1=>$v1){
                    if($v1['type'] == 'FILE'){
                        $str = '';
                        foreach($v1['value'] as $k2=>$v2){
                            $str .= $k2.'@@'.$v2['href'].'@@'.$v2['status'].'--';
                        }
                        $update_info[$k]['sublist'][$k1]['value'] = rtrim($str,'--');
                    }
                }
            }else{
                foreach($v['subvalue'] as $k1=>$v1){
                    foreach($v1['info'] as $k2=>$v2){
                        if($v2['type'] == 'FILE'){
                            $str = '';
                            foreach($v2['value'] as $k3=>$v3){
                                $str .= $k3.'@@'.$v3['href'].'@@'.$v3['status'].'--';
                            }
                            $update_info[$k]['subvalue'][$k1]['info'][$k2]['value'] = rtrim($str,'--');
                        }
                    }
                }
            }
        }
        //dump($update_info);exit;
        return $update_info;
    }

}